package com.bot.edu.topicsystem.interviewquestionbank.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bot.edu.informationmanage.major.entity.Major;
import com.bot.edu.informationmanage.major.service.IMajorService;
import com.bot.edu.informationmanage.majortype.entity.MajorType;
import com.bot.edu.informationmanage.majortype.service.IMajorTypeService;
import com.bot.edu.topicsystem.interviewquestionbank.entity.MajorVo;
import com.bot.edu.utils.RemoveLabel;
import com.bot.edu.utils.SnowFlake;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.bot.edu.topicsystem.interviewquestionbank.entity.InterviewQuestionBank;
import com.bot.edu.topicsystem.interviewquestionbank.service.IInterviewQuestionBankService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 题库
 * @Author: jeecg-boot
 * @Date:   2022-04-08
 * @Version: V1.0
 */
@Api(tags="题库")
@RestController
@RequestMapping("/interviewquestionbank/interviewQuestionBank")
@Slf4j
public class InterviewQuestionBankController extends JeecgController<InterviewQuestionBank, IInterviewQuestionBankService> {
	@Autowired
	private IInterviewQuestionBankService interviewQuestionBankService;
	@Autowired
	private IMajorTypeService majorTypeService;
	@Autowired
	private IMajorService majorService;
	
	/**
	 * 分页列表查询
	 *
	 * @param interviewQuestionBank
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "题库-分页列表查询")
	@ApiOperation(value="题库-分页列表查询", notes="题库-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<InterviewQuestionBank>> queryPageList(InterviewQuestionBank interviewQuestionBank,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<InterviewQuestionBank> queryWrapper = QueryGenerator.initQueryWrapper(interviewQuestionBank, req.getParameterMap());
		Page<InterviewQuestionBank> page = new Page<InterviewQuestionBank>(pageNo, pageSize);
		IPage<InterviewQuestionBank> pageList = interviewQuestionBankService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param interviewQuestionBank
	 * @return
	 */
	@AutoLog(value = "题库-添加")
	@ApiOperation(value="题库-添加", notes="题库-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody InterviewQuestionBank interviewQuestionBank) {
		String id = SnowFlake.getId();
		interviewQuestionBank.setTopicId(id);
		interviewQuestionBank.setDelFlag("0");
		String s = RemoveLabel.removeHtmlTag(interviewQuestionBank.getAnswer());
		interviewQuestionBank.setAnswer(s);
		String s1 = RemoveLabel.removeHtmlTag(interviewQuestionBank.getTopicAbout());
		interviewQuestionBank.setTopicAbout(s1);
		interviewQuestionBankService.save(interviewQuestionBank);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param interviewQuestionBank
	 * @return
	 */
	@AutoLog(value = "题库-编辑")
	@ApiOperation(value="题库-编辑", notes="题库-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody InterviewQuestionBank interviewQuestionBank) {
		String s = RemoveLabel.removeHtmlTag(interviewQuestionBank.getAnswer());
		interviewQuestionBank.setAnswer(s);
		String s1 = RemoveLabel.removeHtmlTag(interviewQuestionBank.getTopicAbout());
		interviewQuestionBank.setTopicAbout(s1);
		interviewQuestionBankService.updateById(interviewQuestionBank);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "题库-通过id删除")
	@ApiOperation(value="题库-通过id删除", notes="题库-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		interviewQuestionBankService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "题库-批量删除")
	@ApiOperation(value="题库-批量删除", notes="题库-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.interviewQuestionBankService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "题库-通过id查询")
	@ApiOperation(value="题库-通过id查询", notes="题库-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<InterviewQuestionBank> queryById(@RequestParam(name="id",required=true) String id) {
		InterviewQuestionBank interviewQuestionBank = interviewQuestionBankService.getById(id);
		if(interviewQuestionBank==null) {
			return Result.error("未找到对应数据",null);
		}
		return Result.OK(interviewQuestionBank);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param interviewQuestionBank
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, InterviewQuestionBank interviewQuestionBank) {
        return super.exportXls(request, interviewQuestionBank, InterviewQuestionBank.class, "题库");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, InterviewQuestionBank.class);
    }

	 /**
	  * 数型控件数据
	  * @return
	  */
	 @RequestMapping(value = "/getTreeTopic", method = RequestMethod.GET)
	 public Result<?> getTreeTopic(){
	 	 List<MajorVo> majorVoList = new ArrayList<>();
		 List<Major> majorList = majorService.list();

		 for (int i = 0; i < majorList.size(); i++) {
			 MajorVo majorVo = new MajorVo();
			 majorVo.setKey(majorList.get(i).getMajorId());
			 majorVo.setTitle(majorList.get(i).getMajorName());
			 majorVo.setType(1+"");

			 QueryWrapper<MajorType> majorTypeQueryWrapper = new QueryWrapper<MajorType>();
			 majorTypeQueryWrapper.lambda().eq(MajorType::getMajorId, majorList.get(i).getMajorId());

			 List<MajorType> majorTypeList = majorTypeService.list(majorTypeQueryWrapper);
			 List<MajorVo> majorVoTypeList = new ArrayList<>();
			 for (int j = 0; j < majorTypeList.size(); j++) {
				 MajorVo majorVoType = new MajorVo();
				 majorVoType.setKey(majorTypeList.get(j).getMajorTypeId());
				 majorVoType.setTitle(majorTypeList.get(j).getMajorTypeName());
				 majorVoType.setType(2+"");
				 majorVoTypeList.add(majorVoType);
			 }
			 majorVo.setChildren(majorVoTypeList);
			 majorVoList.add(majorVo);
		 }
		 return Result.ok(majorVoList);
	 }

	 /**
	  * 根据题目编号获取数据
	  * @param topicTypeId
	  * @return
	  */
	 @RequestMapping(value = "/getRightTopic", method = RequestMethod.GET)
	 public Result<?> getRightTopic(String topicTypeId){
		 QueryWrapper<InterviewQuestionBank> interviewQuestionBankQueryWrapper = new QueryWrapper<>();
		 interviewQuestionBankQueryWrapper.lambda().eq(InterviewQuestionBank::getTopicTypeId,topicTypeId);
		 List<InterviewQuestionBank> topicList = interviewQuestionBankService.list(interviewQuestionBankQueryWrapper);
		 return Result.ok(topicList);
	 }

	 /**
	  * 根据id编辑
	  * @param id
	  * @return
	  */
		@PostMapping(value = "/updateById")
		public Result<String> updateById(@RequestParam(name="id") String id,InterviewQuestionBank interviewQuestionBank) {
			interviewQuestionBankService.updateById(interviewQuestionBank);
			return Result.OK("编辑成功！！！");
		}

	 /**
	  * 根据id编辑
	  * @param id
	  * @return
	  */
	 @PostMapping(value = "/selectByQuestionId")
	 public Result<?> selectById(@RequestParam(name="id") String id) {

		 QueryWrapper<InterviewQuestionBank> questionWrapper = new QueryWrapper<>();
		 questionWrapper.lambda().eq(InterviewQuestionBank::getId,id);
		 InterviewQuestionBank question = interviewQuestionBankService.getOne(questionWrapper);
		 return Result.OK(question);
	 }
 }
